﻿Public Class Conceptos

    Private oMaestra As New CMaestras
    Private dtConcepto As New DataTable
    Private idConcepto As Integer
    Private aereo As Boolean = False
    Private maritimo As Boolean = False
    Private guias As Boolean = False

    Private Sub Conceptos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CargarGrilla()
        Me.CargarTipo()
    End Sub

    Private Sub CargarGrilla()
        Try
            'Trae los permisos del usuario para aereo y maritimo
            Me.aereo = False
            Me.maritimo = False
            Me.guias = False
            Dim ds2 As New DataSet
            Dim oAut As New CAutenticacion
            ds2 = oAut.GetPermisos(My.Forms.Principal.IdUsuario)
            For i As Integer = 0 To ds2.Tables(1).Rows.Count - 1
                If (ds2.Tables(1).Rows(i)("AEREO")) Then
                    Me.aereo = True
                End If
                If (ds2.Tables(1).Rows(i)("MARITIMO")) Then
                    Me.maritimo = True
                End If
                If (ds2.Tables(1).Rows(i)("GUIAS")) Then
                    Me.guias = True
                End If
            Next

            If Not (Me.aereo) Then
                Me.chkAereo.Enabled = False
            End If
            If Not (Me.maritimo) Then
                Me.chkMaritimo.Enabled = False
            End If
            If Not (Me.guias) Then
                Me.chkGuias.Enabled = False
            End If

            Dim ds As New DataSet
            ds = Me.oMaestra.GetConceptos(My.Forms.Principal.IdUsuario, Me.aereo, Me.maritimo, Me.guias)
            Me.dtConcepto = ds.Tables(0).Copy

            'Llena grilla de las ciudades
            If (Me.dtConcepto.Rows.Count > 0) Then
                Me.grdConcepto.DataSource = Nothing
                Me.grdConcepto.Columns.Clear()
                Me.grdConcepto.DataSource = Me.dtConcepto

                Dim iconoEDITAR As New DataGridViewImageColumn()
                iconoEDITAR.ValuesAreIcons = False
                iconoEDITAR.Image = My.Resources.editar
                iconoEDITAR.HeaderText = "EDITAR"
                iconoEDITAR.Name = "EDITAR"
                Me.grdConcepto.Columns.Add(iconoEDITAR)

                Dim iconoELIMINAR As New DataGridViewImageColumn()
                iconoELIMINAR.ValuesAreIcons = False
                iconoELIMINAR.Image = My.Resources.eliminar
                iconoELIMINAR.HeaderText = "ELIMINAR"
                iconoELIMINAR.Name = "ELIMINAR"
                Me.grdConcepto.Columns.Add(iconoELIMINAR)

                Me.grdConcepto.Columns("ID").Visible = False
                Me.grdConcepto.Columns("DUEAGENT").Visible = False
                Me.grdConcepto.Columns("EDITAR").Width = 60
                Me.grdConcepto.Columns("ELIMINAR").Width = 70
            Else
                Me.grdConcepto.DataSource = Nothing
                Me.grdConcepto.Columns.Clear()
                MessageBox.Show("No existen conceptos", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        Catch ex As Exception
            MessageBox.Show("Ocurrió un error al cargar los conceptos: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub CargarTipo()
        'Construye tabla para cargar el combo de los tipos
        Dim dt As New DataTable
        dt.Columns.Add("ID")
        dt.Columns.Add("TIPO")
        Dim arrValues(1) As String
        arrValues(0) = "0"
        arrValues(1) = "Seleccione..."
        dt.Rows.Add(arrValues)
        arrValues(0) = "FIJO"
        arrValues(1) = "Valor fijo"
        dt.Rows.Add(arrValues)
        arrValues(0) = "PORCENTAJE"
        arrValues(1) = "Porcentaje"
        dt.Rows.Add(arrValues)
        arrValues(0) = "LIBRE"
        arrValues(1) = "Libre"
        dt.Rows.Add(arrValues)
        arrValues(0) = "KILOS"
        arrValues(1) = "Por kilos"
        dt.Rows.Add(arrValues)

        Me.drpTipo.DataSource = dt
        Me.drpTipo.ValueMember = dt.Columns(0).ColumnName
        Me.drpTipo.DisplayMember = dt.Columns(1).ColumnName

    End Sub

    Private Sub EliminarConcepto(ByVal id As Integer)
        Try
            If (MessageBox.Show("¿Realmente desea eliminar este concepto?", "Eliminar concepto", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                Cursor = Cursors.WaitCursor
                Me.oMaestra.Eliminar(id, CMaestras.Tabla.Concepto)
                MessageBox.Show("El concepto ha sido eliminado", "Concepto eliminado", MessageBoxButtons.OK, MessageBoxIcon.Information)

                Cursor = Cursors.Default
            End If
        Catch ex As Exception
            Cursor = Cursors.Default
            MessageBox.Show("NO se eliminó el concepto: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
        Me.CargarGrilla()
    End Sub

    Private Sub btnCancelarCiudad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelarCiudad.Click
        Me.idConcepto = 0
        Me.txtConcepto.Text = ""
        Me.txtValor.Text = ""
        If (Me.drpTipo.Items.Count = 0) Then
            Me.drpTipo.Items.Add("No hay registros")
        End If
        Me.drpTipo.SelectedIndex = 0
        Me.chkIVA.Checked = False
        Me.txtValorMinimo.Text = ""
    End Sub

    Private Sub btnListoCiudad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListoCiudad.Click
        If (Me.Validar()) Then
            Try
                Cursor = Cursors.WaitCursor
                Dim valor As Double = 0
                Dim iva As Integer = 0
                Dim minimo As Double = 0
                Dim aereo As Integer = 0
                Dim maritimo As Integer = 0
                Dim tercero As Integer = 0
                Dim guia As Integer = 0
                Dim dueAgent As Integer = 0
                If (Me.txtValor.Text <> "") Then
                    valor = CUtil.FormatearNumero(Me.txtValor.Text)
                End If
                If (Me.chkIVA.Checked) Then
                    iva = 1
                End If
                If (Me.txtValorMinimo.Text <> "") Then
                    minimo = CUtil.FormatearNumero(Me.txtValorMinimo.Text)
                End If
                If (Me.chkAereo.Checked) And (Me.aereo) Then
                    aereo = 1
                End If
                If (Me.chkMaritimo.Checked) And (Me.maritimo) Then
                    maritimo = 1
                End If
                If (Me.chkTerceros.Checked) Then
                    tercero = 1
                End If
                If (Me.chkGuias.Checked) Then
                    guia = 1
                    If (Me.rbtnDueAgent.Checked) Then
                        dueAgent = 1
                    End If
                End If                

                Dim result As String = Me.oMaestra.ActualizarConcepto(Me.idConcepto, Trim(Me.txtConcepto.Text), Me.drpTipo.SelectedValue, valor, iva, minimo, aereo, maritimo, tercero, guia, dueAgent)
                'Si esta abierto el formulario de loas hijas
                If (My.Forms.Hijas.Visible) Then
                    My.Forms.Hijas.CargarConceptos()
                End If
                If (My.Forms.HijasMaritimo.Visible) Then
                    My.Forms.HijasMaritimo.CargarConceptos()
                End If
                If (My.Forms.HijasTercero.Visible) Then
                    My.Forms.HijasTercero.CargarConceptos()
                End If
                If (My.Forms.HijasAereoExportacion.Visible) Then
                    My.Forms.HijasAereoExportacion.CargarConceptos()
                End If
                If (My.Forms.Guias.Visible) Then
                    My.Forms.Guias.CargarConceptos()
                End If

                Cursor = Cursors.Default

                MessageBox.Show(result, "Concepto", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                Cursor = Cursors.Default
                MessageBox.Show("Ocurrió un error al guardar el concepto: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End Try

            Me.idConcepto = 0
            Me.txtConcepto.Text = ""
            Me.drpTipo.SelectedValue = "0"
            Me.txtValor.Text = ""
            Me.chkIVA.Checked = False
            Me.txtValorMinimo.Text = ""
            Me.chkAereo.Checked = False
            Me.chkMaritimo.Checked = False
            Me.chkTerceros.Checked = False
            Me.chkGuias.Checked = False
            Me.CargarGrilla()
        End If
    End Sub

    Private Function Validar() As Boolean
        Dim mensaje As String = ""

        If (Trim(Me.txtConcepto.Text).Length = 0) Then
            mensaje += "- Debe escribir la descripción del concepto" & Chr(13)
        End If
        If (Me.drpTipo.SelectedValue = "0") Then
            mensaje += "- Debe seleccionar el tipo" & Chr(13)
        End If

        If (Me.drpTipo.SelectedValue = "FIJO" Or Me.drpTipo.SelectedValue = "PORCENTAJE") Then
            If (Trim(Me.txtValor.Text).Length = 0) Then
                mensaje += "- Debe escribir el valor del concepto" & Chr(13)
            Else
                If Not IsNumeric(Me.txtValor.Text) Then
                    mensaje += "- El valor debe ser numérico" & Chr(13)
                End If
            End If
        End If

        'Si se selecciona concepto para guias el tipo solo puede ser fijo
        If (Me.chkGuias.Checked And Me.drpTipo.SelectedValue <> "LIBRE" And Me.drpTipo.SelectedValue <> "FIJO") Then
            mensaje += "- Los conceptos para guías solamente pueden ser de tipo 'Fijo' o 'Libre'" & Chr(13)
        End If

        If (Me.txtValorMinimo.Text <> "") Then
            If Not IsNumeric(Me.txtValorMinimo.Text) Then
                mensaje += "- El valor mínimo debe ser numérico" & Chr(13)
            End If
        End If

        'Si el concepto es para guias se valida que se haya seleccionado algun radio button
        If (Me.chkGuias.Checked) Then
            If (Not (Me.rbtnDueAgent.Checked)) And (Not (Me.rbtnDueCarrier.Checked)) Then
                mensaje += "- Para los conceptos de las guías debe definir si es 'Due To Carrier' o 'Due To Agent'" & Chr(13)
            End If
        End If

        If (mensaje.Length > 0) Then
            MessageBox.Show(mensaje, "Datos incompletos", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return False
        Else
            Return True
        End If
    End Function

#Region "Eventos de la grilla"

    Private Sub grdConcepto_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdConcepto.CellDoubleClick
        '*** Permite EDITAR o ELIMINAR
        'Se obtiene la info del registro
        Dim id As Integer = Me.grdConcepto.Rows(e.RowIndex).Cells("ID").Value
        Dim concepto As String = Me.grdConcepto.Rows(e.RowIndex).Cells("CONCEPTO").Value
        Dim tipo As String = Me.grdConcepto.Rows(e.RowIndex).Cells("TIPO").Value
        Dim valor As Double = Me.grdConcepto.Rows(e.RowIndex).Cells("VALOR").Value
        Dim iva As Boolean = Me.grdConcepto.Rows(e.RowIndex).Cells("IVA").Value
        Dim minimo As Double = Me.grdConcepto.Rows(e.RowIndex).Cells("MINIMO").Value
        Dim aereo As Boolean = Me.grdConcepto.Rows(e.RowIndex).Cells("AEREO").Value
        Dim maritimo As Boolean = Me.grdConcepto.Rows(e.RowIndex).Cells("MARITIMO").Value
        Dim terceros As Boolean = Me.grdConcepto.Rows(e.RowIndex).Cells("TERCEROS").Value
        Dim guias As Boolean = Me.grdConcepto.Rows(e.RowIndex).Cells("GUIAS").Value
        Dim dueAgent As Integer = CInt(Me.grdConcepto.Rows(e.RowIndex).Cells("DUEAGENT").Value.ToString())

        Select Case Me.grdConcepto.Columns(e.ColumnIndex).Name
            Case "EDITAR"
                Me.idConcepto = id
                Me.txtConcepto.Text = concepto
                Me.drpTipo.SelectedValue = tipo
                Me.txtValor.Text = valor
                Me.txtValorMinimo.Text = minimo

                Me.chkIVA.Checked = False
                Me.chkAereo.Checked = False
                Me.chkMaritimo.Checked = False
                Me.chkTerceros.Checked = False
                Me.chkGuias.Checked = False
                If (iva) Then
                    Me.chkIVA.Checked = True                
                End If
                If (aereo) Then
                    Me.chkAereo.Checked = True
                End If
                If (maritimo) Then
                    Me.chkMaritimo.Checked = True
                End If
                If (terceros) Then
                    Me.chkTerceros.Checked = True
                End If
                If (guias) Then
                    Me.chkGuias.Checked = True
                    If (dueAgent = 1) Then
                        Me.rbtnDueAgent.Checked = True
                        Me.rbtnDueCarrier.Checked = False
                    Else
                        Me.rbtnDueAgent.Checked = False
                        Me.rbtnDueCarrier.Checked = True
                    End If
                End If

            Case "ELIMINAR"
                Me.EliminarConcepto(id)
        End Select
    End Sub
    Private Sub grdConcepto_CellMouseEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdConcepto.CellMouseEnter
        '*** Coloca la manito cuando el mouse entra en las celdas de EDITAR y ELIMINAR
        If (Me.grdConcepto.Columns(e.ColumnIndex).Name = "ELIMINAR" Or Me.grdConcepto.Columns(e.ColumnIndex).Name = "EDITAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub
    Private Sub grdConcepto_CellMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdConcepto.CellMouseMove
        If (Me.grdConcepto.Columns(e.ColumnIndex).Name = "ELIMINAR" Or Me.grdConcepto.Columns(e.ColumnIndex).Name = "EDITAR") Then
            Cursor = Cursors.Hand
        Else
            Cursor = Cursors.Default
        End If
    End Sub

#End Region

    Private Sub chkGuias_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkGuias.CheckedChanged
        If (Me.chkGuias.Checked) Then
            Me.rbtnDueAgent.Visible = True
            Me.rbtnDueCarrier.Visible = True
            Me.rbtnDueAgent.Checked = True
            Me.rbtnDueCarrier.Checked = False
        Else
            Me.rbtnDueAgent.Visible = False
            Me.rbtnDueCarrier.Visible = False
        End If
    End Sub
End Class